import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Project		: Nerdle
 * File 		: Topten.java			Version: 
 * Date 		: 3:09:07 AM
 * Author		: kkopiec
 * Description	:
 *
 */

/**
 * @author kkopiec
 * 
 */
public class Topten implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2186531856214216879L;
	protected HashMap<String, Integer> position;
	protected int maxSize = 10;

	public Topten() {
		position = new HashMap<String, Integer>();

	}

	public void add(String name, Integer score) {
		if (position.size() > 10) {
			if (score > getLowest().getValue()) {
				position.remove(getLowest());
				position.put(name, new Integer(score));

			}
		} else {
			position.put(name, new Integer(score));

		}
	}

	public Map.Entry<String, Integer> getLowest() {
		int lowest = Integer.MAX_VALUE;
		Map.Entry<String, Integer> weakest = null;
		for (Map.Entry<String, Integer> entry : position.entrySet()) {
			if (entry.getValue() < lowest) {
				lowest = entry.getValue();
				weakest = entry;
			}
		}
		return weakest;
	}

	public ArrayList<String> getTopTen() {
		ArrayList<String> results = new ArrayList<String>();
		HashMap<String,Integer> tmp = new HashMap<String, Integer>(position);
		int size = position.size();
		for (int i = 0; i < size; i++){
			Map.Entry<String, Integer> entry = getLowest();
			results.add(0, entry.getKey() + " --- " + entry.getValue());
			position.remove(entry.getKey());
		}
		position = tmp;
		return results;
	}

}
